package b.n;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* compiled from: Lifecycling */
public class j {
    public static Map<Class, Integer> a = new HashMap();
    public static Map<Class, List<Constructor<? extends c>>> b = new HashMap();

    public static c a(Constructor<? extends c> constructor, Object obj) {
        try {
            return (c) constructor.newInstance(new Object[]{obj});
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e2) {
            throw new RuntimeException(e2);
        } catch (InvocationTargetException e3) {
            throw new RuntimeException(e3);
        }
    }

    public static String b(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str.replace(".", "_"));
        stringBuilder.append("_LifecycleAdapter");
        return stringBuilder.toString();
    }

    public static int c(Class<?> cls) {
        Integer num = (Integer) a.get(cls);
        if (num != null) {
            return num.intValue();
        }
        Class cls2 = f.class;
        int i = 1;
        if (cls.getCanonicalName() != null) {
            Object declaredConstructor;
            List list = null;
            try {
                Package packageR = cls.getPackage();
                String canonicalName = cls.getCanonicalName();
                String name = packageR != null ? packageR.getName() : "";
                if (!name.isEmpty()) {
                    canonicalName = canonicalName.substring(name.length() + 1);
                }
                canonicalName = b(canonicalName);
                if (!name.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(name);
                    stringBuilder.append(".");
                    stringBuilder.append(canonicalName);
                    canonicalName = stringBuilder.toString();
                }
                declaredConstructor = Class.forName(canonicalName).getDeclaredConstructor(new Class[]{cls});
                if (!declaredConstructor.isAccessible()) {
                    declaredConstructor.setAccessible(true);
                }
            } catch (ClassNotFoundException unused) {
                declaredConstructor = null;
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
            if (declaredConstructor != null) {
                b.put(cls, Collections.singletonList(declaredConstructor));
            } else {
                boolean booleanValue;
                a aVar = a.c;
                Boolean bool = (Boolean) aVar.b.get(cls);
                if (bool != null) {
                    booleanValue = bool.booleanValue();
                } else {
                    try {
                        Method[] declaredMethods = cls.getDeclaredMethods();
                        for (Method annotation : declaredMethods) {
                            if (((n) annotation.getAnnotation(n.class)) != null) {
                                aVar.a(cls, declaredMethods);
                                booleanValue = true;
                                break;
                            }
                        }
                        aVar.b.put(cls, Boolean.FALSE);
                        booleanValue = false;
                    } catch (NoClassDefFoundError e2) {
                        throw new IllegalArgumentException("The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.", e2);
                    }
                }
                if (!booleanValue) {
                    Class superclass = cls.getSuperclass();
                    Object obj = (superclass == null || !cls2.isAssignableFrom(superclass)) ? null : 1;
                    if (obj != null) {
                        if (c(superclass) != 1) {
                            list = new ArrayList((Collection) b.get(superclass));
                        }
                    }
                    for (Class cls3 : cls.getInterfaces()) {
                        Object obj2 = (cls3 == null || !cls2.isAssignableFrom(cls3)) ? null : 1;
                        if (obj2 != null) {
                            if (c(cls3) == 1) {
                                break;
                            }
                            if (list == null) {
                                list = new ArrayList();
                            }
                            list.addAll((Collection) b.get(cls3));
                        }
                    }
                    if (list != null) {
                        b.put(cls, list);
                    }
                }
            }
            i = 2;
        }
        a.put(cls, Integer.valueOf(i));
        return i;
    }
}
